package com.lancoo.evaluate.util;

import com.lancoo.evaluate.bean.Evaluate;
import com.lancoo.evaluate.bean.EvaluateResult;

import org.json.JSONException;
import org.json.JSONObject;

public class ResultParse {
    public static EvaluateResult parse(Evaluate evaluate, String result) {
        EvaluateResult evaluateResult = new EvaluateResult();
        evaluateResult.setWavPath(evaluate.getWavPath());
        try {
            JSONObject returnJson = new JSONObject(result);
            if (returnJson.has("error")) {
                String error = returnJson.getString("error");
                evaluateResult.setError(error);
            } else if (returnJson.has("result")) {
                JSONObject resultJson = returnJson.getJSONObject("result");

                // 得分
                if (resultJson.has("overall")) {
                    String overall = resultJson.getString("overall");
                    double totalScore = evaluate.getTotalScore();
                    double score = Double.parseDouble(overall) / 100 * totalScore;
                    evaluateResult.setScore(score);
                }

                // 时长
                String duration = resultJson.getString("duration");
                try {
                    long millisecond = Long.parseLong(duration);
                    evaluateResult.setDuration(millisecond);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

                // 流利度
                if (resultJson.has("fluency")) {
                    int fluency = resultJson.getInt("fluency");
                    evaluateResult.setFluency(fluency);
                } else {
                    evaluateResult.setFluency(-1);
                }
                // 完整度
                if (resultJson.has("integrity")) {
                    int integrity = resultJson.getInt("integrity");
                    evaluateResult.setIntegrity(integrity);
                } else {
                    evaluateResult.setIntegrity(-1);
                }
                // 准确度
                if (resultJson.has("overall")) {// 直接取得分
                    String overall = resultJson.getString("overall");
                    int pronunciation = (int) Float.parseFloat(overall);
                    evaluateResult.setAccuracy(pronunciation);
                } else {
                    evaluateResult.setAccuracy(-1);
                }
                // 韵律性
                if (resultJson.has("rhythm")) {
                    int rhythm = resultJson.getInt("rhythm");
                    evaluateResult.setRhythm(rhythm);
                } else {
                    evaluateResult.setRhythm(-1);
                }

                // 评测分析
                if (resultJson.has("words")) {
                    String words = resultJson.getString("words");
                    evaluateResult.setAnalysis(words);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return evaluateResult;
    }
}
